package com.li.client.management;

import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSProtocol;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public abstract class LibGridDataSource extends DataSource
{
	public static final String CLIENT_CONTEXT = "clientContext";
	
	LibGridDataSource()
	{
		createDataSourceFields();
		setDataProtocol(DSProtocol.CLIENTCUSTOM);
		setDataFormat(DSDataFormat.CUSTOM);
		setClientOnly(true);
	}
	
	protected abstract void createDataSourceFields();
	
	
	protected final Object transformRequest(DSRequest request)
	{
		String requestId = request.getRequestId();
		DSResponse response = new DSResponse();
		response.setAttribute(CLIENT_CONTEXT, request.getAttributeAsObject(CLIENT_CONTEXT));
		response.setStatus(0);
		switch (request.getOperationType())
		{
		case FETCH:
			executeFetch(requestId, request, response);
			break;
		case ADD:
			break;
		case UPDATE:
			break;
		case REMOVE:
			break;
		case CUSTOM:
			break;
		default:
			// Operation not implemented.
			break;
		}
		return request.getData();
	}

	protected abstract void executeFetch(String requestId, DSRequest request, DSResponse response);
	
	protected void setNullRecords(DSRequest request, DSResponse response)
	{
		response.setData(new ListGridRecord[0]);
		response.setTotalRows(0);
		processResponse(request.getRequestId(), response);
	}
	
	protected void executeUpdate(ListGridRecord record, String fieldName, Object value)
	{
		
	}
	
}
